package com.kejiguan.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kejiguan.ui.adapter.UserManagerListAdapter;
import com.kejiguan.ui.dialog.AddUserDialog;
import com.kejiguan.ui.dialog.AddUserDialogUtil;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyUserDialog;
import com.kejiguan.ui.dialog.ModifyUserDialogUtil;
import com.kejiguan.ui.model.MenuItem;
import com.kejiguan.ui.model.UserManagerModer;
import com.kejiguan.ui.presenter.UserManagerFragmentPresenter;
import com.kejiguan.ui.response.AddUserResponse;
import com.kejiguan.ui.response.DelUserResponse;
import com.kejiguan.ui.response.ModifyUserResponse;
import com.kejiguan.ui.response.QueryMenuResponse;
import com.kejiguan.ui.response.UserManagerListResponse;
import com.kejiguan.ui.widget.MyLinearLayoutManager;
import com.kejiguan.ui.widget.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

/**
 * Created by YYY on 2016/4/30.
 */
public class UserManagerFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener,AddUserDialog.OnNewUserAddListener,ModifyUserDialog.OnUserModifyListener{


    private View rootView;

    private View addBtn;
    private RecyclerView userListView;
    private UserManagerFragmentPresenter presenter;

    private UserManagerListAdapter adapter;

    private List<MenuItem> menuItems;
    private UserManagerModer delUserManagerModer;
    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        presenter=new UserManagerFragmentPresenter();
        presenter.attach(getContext());
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (null!=menuItems)
        {
            presenter.querySysUsers();
        }else {
            presenter.queryMenus();
        }

    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {

         if (rootView==null)
         {
             rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_user_manager_layout, container, false);
             addBtn=rootView.findViewById(R.id.add_btn);
             userListView= (RecyclerView) rootView.findViewById(R.id.user_list);
             addBtn.setOnClickListener(this);

             MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(getContext());
             layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
             userListView.setLayoutManager(layoutManager);
             userListView.addItemDecoration(new SpacesItemDecoration(40,false));
             adapter=new UserManagerListAdapter(getContext(),null);
             adapter.setOnClickListener(this);
             adapter.setOnLongClickListener(this);
             userListView.setAdapter(adapter);
         }
        return rootView;

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UserManagerListResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
            adapter.setUserManagerModers(response.getSysUserList());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DelUserResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
            UserManagerModer userManagerModer=response.getUserManagerModer();
            int count=adapter.getItemCount();
            for (int i=0;i<count;i++)
            {
                if (userManagerModer.getUserName().equals(adapter.getUser(i).getUserName()))
                {
                    adapter.getUserManagerModers().remove(i);
                    adapter.notifyDataSetChanged();
                    break;
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryMenuResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
            menuItems=response.getMenuItems();
            presenter.querySysUsers();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(AddUserResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
//            Toast.makeText(getContext(),"新增成功",Toast.LENGTH_SHORT).show();
            presenter.querySysUsers();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ModifyUserResponse response)
    {
        if ("0000".equals(response.getRetCode()))
        {
//            Toast.makeText(getContext(),"修改成功",Toast.LENGTH_SHORT).show();
            presenter.querySysUsers();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_btn:
                if (null!=menuItems)
                {
                    Intent intent=new Intent(getContext(),AddUserActivity.class);
                    intent.putExtra("menuItems",(Serializable) menuItems);
                    startActivity(intent);
                }
                break;
            case R.id.user_item:
                if (null!=menuItems)
                {
                    int neItemPosition= (int) v.getTag(R.id.recyclerview_item_position);
                    final UserManagerModer userManagerModer=adapter.getUser(neItemPosition);
                    Intent intent=new Intent(getContext(),ModifyUserActivity.class);
                    intent.putExtra("menuItems",(Serializable) menuItems);
                    intent.putExtra("UserManagerModer",(Serializable) userManagerModer);
                    startActivity(intent);
                }
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                presenter.delUser(delUserManagerModer);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }

    }

    @Override
    public boolean onLongClick(View v) {

        switch (v.getId())
        {
            case R.id.user_item:
                int neItemPosition= (int) v.getTag(R.id.recyclerview_item_position);
                delUserManagerModer=adapter.getUser(neItemPosition);
                CommDialogUtil.getInstance().show(getContext(),"要删除用户"+delUserManagerModer.getUserName()+"吗?",UserManagerFragment.this,"删除","取消",0);
                break;
        }
        return true;
    }

    @Override
    public void onNewUserAdd(UserManagerModer newUser) {

        presenter.addUser(newUser);
    }

    @Override
    public void onUserModify(UserManagerModer userManagerModer) {
        presenter.modifyUser(userManagerModer);
    }
}
